﻿using System;

namespace EmptoolWebApp.Controllers
{
    public class BaseController : System.Web.Mvc.Controller
    {
        protected override IAsyncResult BeginExecuteCore(AsyncCallback callback, object state)
        {
            string cultureName = string.Empty;

            // Get culture from route data
            cultureName = RouteData.Values["culture"] as string;

            // if route data does not have culture information,
            // 1.   get from cookie
            // else default to English
            if (string.IsNullOrWhiteSpace(cultureName))
            {
                if (System.Web.HttpContext.Current.Request.Cookies["SELECTED_LANGUAGE"] != null)
                {
                    cultureName = System.Web.HttpContext.Current.Request.Cookies["SELECTED_LANGUAGE"].Value;
                }
                else
                {
                    cultureName = "en";
                }
            }

            // Modify current thread's cultures
            System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(cultureName);
            System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;

            return base.BeginExecuteCore(callback, state);
        }
    }
}
